package org.wltea.analyzer.query;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.Query;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

public class SWMCQueryBuilder {
    public SWMCQueryBuilder() {
    }

    public static Query create(String fieldName, String keywords, boolean quickMode) {
        if (fieldName != null && keywords != null) {
            List<Lexeme> lexemes = doAnalyze(keywords);
            Query _SWMCQuery = getSWMCQuery(fieldName, lexemes, quickMode);
            return _SWMCQuery;
        } else {
            throw new IllegalArgumentException("参数 fieldName 、 keywords 不能为null.");
        }
    }

    private static List<Lexeme> doAnalyze(String keywords) {
        List<Lexeme> lexemes = new ArrayList();
        IKSegmenter ikSeg = new IKSegmenter(new StringReader(keywords), true);

        try {
            Lexeme l = null;

            while((l = ikSeg.next()) != null) {
                lexemes.add(l);
            }
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return lexemes;
    }

    private static Query getSWMCQuery(String fieldName, List<Lexeme> lexemes, boolean quickMode) {
        StringBuffer keywordBuffer = new StringBuffer();
        StringBuffer keywordBuffer_Short = new StringBuffer();
        int lastLexemeLength = 0;
        int lastLexemeEnd = -1;
        int shortCount = 0;
        int totalCount = 0;

        Lexeme l;
        for(Iterator var9 = lexemes.iterator(); var9.hasNext(); lastLexemeEnd = l.getEndPosition()) {
            l = (Lexeme)var9.next();
            totalCount += l.getLength();
            if (l.getLength() > 1) {
                keywordBuffer_Short.append(' ').append(l.getLexemeText());
                shortCount += l.getLength();
            }

            if (lastLexemeLength == 0) {
                keywordBuffer.append(l.getLexemeText());
            } else if (lastLexemeLength == 1 && l.getLength() == 1 && lastLexemeEnd == l.getBeginPosition()) {
                keywordBuffer.append(l.getLexemeText());
            } else {
                keywordBuffer.append(' ').append(l.getLexemeText());
            }

            lastLexemeLength = l.getLength();
        }

        QueryParser qp = new QueryParser(fieldName, new StandardAnalyzer());
        qp.setDefaultOperator(QueryParser.AND_OPERATOR);
        qp.setAutoGeneratePhraseQueries(true);
        Query q;
        if (quickMode && (float)shortCount * 1.0F / (float)totalCount > 0.5F) {
            try {
                q = qp.parse(keywordBuffer_Short.toString());
                return q;
            } catch (ParseException var12) {
                var12.printStackTrace();
            }
        } else if (keywordBuffer.length() > 0) {
            try {
                q = qp.parse(keywordBuffer.toString());
                return q;
            } catch (ParseException var11) {
                var11.printStackTrace();
            }
        }

        return null;
    }
}
